package com.shop.cereshop.business.service.redis.impl;

import com.shop.cereshop.business.dao.redis.CereRedisKeyDAO;
import com.shop.cereshop.business.service.redis.CereRedisKeyServcice;
import com.shop.cereshop.commons.domain.redis.CereRedisKey;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CereRedisKeyServciceImpl implements CereRedisKeyServcice {

    @Autowired
    private CereRedisKeyDAO cereRedisKeyDAO;

    @Override
    public void add(String key, String endTime) throws CoBusinessException {
        CereRedisKey cereRedisKey=new CereRedisKey();
        cereRedisKey.setRedisKey(key);
        cereRedisKey.setEndTime(endTime);
        cereRedisKeyDAO.insert(cereRedisKey);
    }

    @Override
    public void updateByKey(String key, String endTime) throws CoBusinessException {
        CereRedisKey cereRedisKey=new CereRedisKey();
        //查询是否存在数据
        String time = cereRedisKeyDAO.findByKey(key);
        if(!EmptyUtils.isEmpty(time)){
            //更新
            cereRedisKey.setRedisKey(key);
            cereRedisKey.setEndTime(endTime);
            cereRedisKeyDAO.updateByKey(cereRedisKey);
        }else {
            //新增
            cereRedisKey.setRedisKey(key);
            cereRedisKey.setEndTime(endTime);
            cereRedisKeyDAO.insert(cereRedisKey);
        }
    }

    @Override
    public void deleteByKey(String key) throws CoBusinessException {
        cereRedisKeyDAO.deleteByKey(key);
    }

    @Override
    public String findByKey(String key) {
        return cereRedisKeyDAO.findByKey(key);
    }
}
